//
//  KeyBoardView.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/11.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class KeyBoardView: UIView {

    
    var array : Array<UIButton> = []
    weak var ownerField : UITextField?
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        for i in 1 ..< 13 {
            let button = numberView(number: i)
            self.addSubview(button)
        }
        let separator1 = UIView.init(frame: CGRect.init(x: frame.size.width / 3, y: 0, width: YXWidthScale, height: frame.size.height))
        separator1.backgroundColor = UIColor.colorWithHexString(hex: "f2f2f2")
        self.addSubview(separator1)
        let separator2 = UIView.init(frame: CGRect.init(x: 2 * frame.size.width / 3, y: 0, width: YXWidthScale, height: frame.size.height))
        separator2.backgroundColor = UIColor.colorWithHexString(hex: "f2f2f2")
        self.addSubview(separator2)
        
        let separator3 = UIView.init(frame: CGRect.init(x: 0, y: frame.size.height / 4, width: frame.size.width, height: YXHeightScale))
        separator3.backgroundColor = UIColor.colorWithHexString(hex: "f2f2f2")
        self.addSubview(separator3)
        
        let separator4 = UIView.init(frame: CGRect.init(x: 0, y: frame.size.height / 2, width: frame.size.width, height: YXHeightScale))
        separator4.backgroundColor = UIColor.colorWithHexString(hex: "f2f2f2")
        self.addSubview(separator4)
        
        let separator5 = UIView.init(frame: CGRect.init(x: 0, y: 3 * frame.size.height / 4, width: frame.size.width, height: YXHeightScale))
        separator5.backgroundColor = UIColor.colorWithHexString(hex: "f2f2f2")
        self.addSubview(separator5)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func num(button : UIButton) -> Void {
        if let number = array.index(of: button) {
            if number == 9 {
                guard ownerField?.text == nil else {
                    if shouldChange(string: ".") {
                        ownerField!.text = ownerField!.text! + "."
                    }
                    return
                }
            } else if number == 10 {
                guard ownerField?.text == nil else {
                    if shouldChange(string: "0") {
                        ownerField!.text = ownerField!.text! + "0"
                    }
                    return
                }
            } else if number == 11 {
                guard ownerField?.text == nil else {
                    if ownerField!.text!.characters.count > 0 {
                        let idx = ownerField!.text?.index(before: ownerField!.text!.endIndex)
                        ownerField!.text = ownerField!.text!.substring(to: idx!)
                    }
                    return
                }
            } else {
                let idx = array.index(of: button)! + 1
                let str = String(idx)
                if shouldChange(string: str) {
                    ownerField!.text = ownerField!.text! + str
                }
            }
        }
    }
    
    
    //不许在开头输入两个0
    //不许输入两个.
    //小数点后面只能有两位
    func shouldChange(string : String)->Bool{
        
        let str = ownerField!.text! + string
        
        if ownerField!.text!.range(of: ".") != nil {
            let pre = "^(([1-9][0-9]*)|(([0]\\.\\d{1,2}|[1-9][0-9]*\\.\\d{1,2})))$"
            do {
                let express = try NSRegularExpression.init(pattern: pre, options: .caseInsensitive)
                let result = express.matches(in: str, options: .withTransparentBounds, range: NSRange.init(location: 0, length: str.characters.count))
                if result.count > 0 {
                    return true
                }else{
                    return false
                }
            } catch {
                print(error)
            }
        }
        
        if ownerField!.text! == "0" {
            if string == "." {
                return true
            }
            return false
        }
        
        if ownerField!.text == "" {
            if string == "." {
                return false
            }
        }
        return true
    }
    
    func numberView(number : Int) -> UIButton {
        let btn = UIButton.init(type: .custom)
        let wid = YXDeviceWidth / 3
        let image = UIImage.image(color: UIColor.colorWithHexString(hex: "f2f2f2"), size: CGSize.init(width: wid, height: 40))
        btn.setBackgroundImage(image, for: .highlighted)
        btn.setTitleColor(UIColor.black, for: .normal)
        if number == 10 {
            btn.setTitle(".", for: .normal)
        } else if number == 11 {
            btn.setTitle("0", for: .normal)
        } else if number == 12 {
            let delete = UIImage.load(name: "ic_delete")
            btn.imageEdgeInsets = UIEdgeInsetsMake(10, -25, 10, -25)
            btn.setImage(delete, for: .normal)
        } else {
            btn.setTitle(String(number), for: .normal)
        }
        
        let height = self.bounds.size.height / 4
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 60 * YXWidthScale)
        btn.frame = CGRect.init(x: CGFloat((number - 1) % 3) * wid, y: CGFloat((number - 1) / 3) * height, width: wid, height: height)
        btn.addTarget(self, action: #selector(num(button:)), for: .touchUpInside)
        array.append(btn)
        return btn
    }

}
